import request from '../../../../utils/request';
import moment from 'moment'
Page({
  page: {
    pageSize: 10,
    pageNo: 1,
  },
  data: {
    status: '',
    tabs: [{ text: '全部', key: '' }, { text: '已申请退款', key: 2 }, { text: '已退款', key: 3 }],
    listLoading: 0,
    pullDownRefreshing: false,
    list: [],
  },

  onLoad() {
    this.init(this.data.status);
    this.pullDownRefresh = this.selectComponent('#wr-pull-down-refresh');
  },

  init(status) {
    this.refreshList(status);
  },

  refreshList(status = '') {
    this.page = {
      pageSize: 10,
      pageNo: 1
    };
    this.setData({ status: status, list: [] });

    return Promise.all([
      this.getMoneyList(status, true),
    ]);
  },

  getMoneyList(statusCode = '', reset = false) {
    var that = this;
    const params = {
      pageNo: this.page.pageNo,
      pageSize: this.page.pageSize,
      orderType: 'desc',
      orderField: 'id'
    };
    if (statusCode !== '') {
      params.filter = [{
        field: "flag",
        operator: "EQ",
        value: statusCode
      }]
    }
    this.setData({ listLoading: 1 });
    var url = "car/user/dealer/auctionMoneyPageData";
    request.post(url, params, function (res) {
      let list = [];
      if (res.code == 0) {
        that.page.pageNo++;
        if (res.data && res.data.rows) {

          list = res.data.rows;

          // moment(details.startTime).format("MM月DD日HH:mm")
        }
      }
      return new Promise((resolve) => {
        if (reset) {
          that.setData({ list: [] }, () => resolve());
        } else resolve();
      }).then(() => {
        that.setData({
          list: that.data.list.concat(list),
          listLoading: list.length > 0 ? 0 : 2,
        });
      });
    })
  },

  onReTryLoad() {
    this.getMoneyList(this.data.status);
  },

  onTabChange(e) {
    const { value } = e.detail;
    this.setData({
      status: value,
    });
    this.refreshList(value);
  },

  onRefresh() {
    this.refreshList(this.data.status);
  },

  onPullDownRefresh_(e) {
    const { callback } = e.detail;
    this.setData({ pullDownRefreshing: true });
    this.refreshList(this.data.status)
      .then(() => {
        this.setData({ pullDownRefreshing: false });
        callback && callback();
      })
      .catch((err) => {
        this.setData({ pullDownRefreshing: false });
        Promise.reject(err);
      });
  },

  onRefundMoney(e) {
    const id = e.currentTarget.dataset.id;
    console.log("id:" + id)
    wx.navigateTo({
      url: '/pages/usercenter/money/apply/index?id=' + id,
    })
  },
  triggerChangeData() {
    this.onRefresh();
  }
});
